package com.ruhaly.demo01.controller;

import com.alibaba.fastjson.JSON;
import com.ruhaly.demo01.entity.User;
import com.ruhaly.demo01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/sendBaidu")
public class SendToBaiduController {
    @Resource(name = "userService")
    UserService userService;

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String index(Model model) {
        System.out.println("我来了adfdsfd");
        model.addAttribute("message", "chuanzhi");
        return "/jsp/index";
    }

    @CrossOrigin(origins = {"http://localhost:8089", "http://localhost:63342"})
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody

    public String login(HttpServletResponse httpResponse, Model model, @RequestParam(required = false, name = "name") String name, String address) {

        userService.saveUser(new User(name, address));
        model.addAttribute("name", userService.login(name, address));
//        model.addAttribute("address", address);
        return JSON.toJSONString(model);
    }
}
